﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CapaEntidades;
using System.Data;
using System.Data.SqlClient;

namespace CapaDatos
{
    public class daPersonal
    {
        #region singleton
        private static readonly daPersonal _instancia = new daPersonal();
        public static daPersonal Instancia
        {
            get { return daPersonal._instancia; }
        }
        #endregion

        #region Métodos
        public static List<enPersonal> spListarPersonalAll()
        {
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            List<enPersonal> lstPersonal = null;
            try
            {

                cn = Conexion.getConexion();
                cmd = new SqlCommand("spListarPersonalAll", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                dr = cmd.ExecuteReader();
                lstPersonal = new List<enPersonal>();
                while (dr.Read())
                {

                    enPersonal empleado = new enPersonal();
                    empleado.per_id = Convert.ToInt32(dr["per_id"].ToString());
                    empleado.per_nombres = dr["per_nombres"].ToString();
                    empleado.per_apellidos = dr["per_apellidos"].ToString();
                    empleado.per_dni = dr["per_dni"].ToString();
                    enCargo cargo = new enCargo();
                    cargo.car_descripcion = dr["car_descripcion"].ToString();
                    enUsuario usuario = new enUsuario();
                    usuario.usu_user = dr["usu_user"].ToString();
                    empleado.cargo = cargo;
                    empleado.usuario = usuario;
                    lstPersonal.Add(empleado);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dr.Close();
                cn.Close();
            }
            return lstPersonal;
        }

        public enAlerta spUpdateUsuaPerso(String user, int idPer)
        {
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            enAlerta lstEmpleado = new enAlerta();
            try
            {

                cn = Conexion.getConexion();
                cmd = new SqlCommand("spUpdateUsuaPerso", cn);
                cmd.Parameters.AddWithValue("@per_id", idPer);
                cmd.Parameters.AddWithValue("@usu_user", user);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                dr = cmd.ExecuteReader();
                lstEmpleado.val = -1;
                if (dr.Read())
                {

                    lstEmpleado.val = Convert.ToInt16(dr[0].ToString());
                    lstEmpleado.msg = dr[1].ToString();

                }
            }
            catch (Exception)
            {
                lstEmpleado.val = -1;
                throw;
            }
            finally
            {
                dr.Close();
                cn.Close();
            }
            return lstEmpleado;

        }
        
        public enAlerta spUpdatePassPerso(String pass, int idPer)
        {
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            enAlerta lstEmpleado = new enAlerta();
            try
            {

                cn = Conexion.getConexion();
                cmd = new SqlCommand("spUpdatePassPerso", cn);
                cmd.Parameters.AddWithValue("@per_id", idPer);
                cmd.Parameters.AddWithValue("@usu_pass", pass);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                dr = cmd.ExecuteReader();
                lstEmpleado.val = -1;
                if (dr.Read())
                {

                    lstEmpleado.val = Convert.ToInt16(dr[0].ToString());
                    lstEmpleado.msg = dr[1].ToString();

                }
            }
            catch (Exception)
            {
                lstEmpleado.val = -1;
                throw;
            }
            finally
            {
                dr.Close();
                cn.Close();
            }
            return lstEmpleado;

        }
        #endregion
    }
    }

